<?php

function users(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入用户表和用户详情表到新库 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='users';
    $new_table='user';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //DEBUG 获取所有学校网校
    $dictionary_sql = "SELECT d_id,d_type FROM ".DB::table('dictionary')." WHERE d_id IN ('20203','20204','20205','20206','20207','20208','20209','20248','50534')";
    $dictionary_result = DB::fetch_all($dictionary_sql);
    foreach($dictionary_result AS $key => $value){
        $dictionary_result[$value['d_id']]=$value['d_type'];
    }
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        //DEBUG 导入用户表和用户详情表到新库
        $data = array(
            'user_id'=>$value['user_id'],
            'user_name'=>$value['user_name'],
            'user_password'=>$value['user_password'],
            'ischeck'=>'1',
            'create_dateline'=>$value['user_create_time'],
            'modify_dateline'=>$value['user_modify_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
        $data2 = array(
            'user_id'=>$value['user_id'],
            'am_id_1'=>$value['area_id1'],
            'am_id_2'=>$value['area_id2'],
            'am_id_3'=>$value['area_id3'],
            'sm_id'=>$value['dept_id'],
            'class_id'=>$value['class_id'],
            'grade_num'=>'',
            'class_num'=>'',
            'user_realname'=>$value['user_realname'],
            'user_nickname'=>$value['user_nickname'],
            'user_phone'=>$value['user_phone'],
            'phone_validation'=>$value['phone_validation'],
            'user_avatar'=>$value['user_avatar'],
            'user_sign'=>$value['user_sign'],
            'user_email'=>$value['user_email'],
            'email_validation'=>$value['email_validation'],
            'email_token'=>$value['email_token'],
            'email_token_exptime'=>$value['email_token_exptime'],
            'student_no'=>$value['student_no'],
            'student_school_no'=>'',
            'gender'=>$value['gender'],
            'qq'=>$value['qq'],
            'openid'=>$value['openid'],
            'address'=>$value['address'],
            'about'=>$value['about'],
            'create_dateline'=>$value['user_create_time'],
            'modify_dateline'=>$value['user_modify_time']
        );
        $insert_id = DB::insert('user_detail', $data2, true, true);
        //DEBUG 插入角色关系
        $user_role_relation = array(
            'user_id'=>$value['user_id'],
            'role_id'=>$value['user_role_id'],
        );
        if($value['user_role_id'] == 31){
            //DEBUG 判断是学校还是网校 管理员
            if($dictionary_result[$value['dept_id']]==2){
                //DEBUG 获取机构类型
                $user_role_relation['role_id']=82;
            }
            if($dictionary_result[$value['dept_id']]==1){
                //DEBUG 获取机构类型
                $user_role_relation['role_id']=81;
            }
        }
        DB::insert('user_role_relation', $user_role_relation, true, true);
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入用户表和用户详情表到新库 结束
}

function users_edu(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='users_edu';
    $new_table='user_edu';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        unset($value['ue_d_id']);
        $data = $value;
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id || 1==1){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束  
}

function classs(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='class';
    $new_table='class_main';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        if($value['class_graduate']){
            $value['ishistory']=1;
        }else{
            $value['ishistory']=0;
        }
        $value['master_user_id']=$value['creator_id'];
        $value['sm_id']=$value['dept_id'];
        unset($value['dept_id']);
        $data = $value;
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function class_member(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='class_member';
    $new_table='class_member';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $value['id']=$value['cm_id'];
        unset($value['cm_id']);
        $data = $value;
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function dictionary(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='dictionary';
    $new_table='area_main';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE d_root_id=100 AND d_type=0 AND d_id > 100 AND isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE d_root_id=100 AND d_type=0 AND d_id > 100 AND isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        if($value['d_parent_id']==100){
            $value['d_parent_id']=0;
        }
        $data = array(
            'am_id'=>$value['d_id'],
            'am_name'=>$value['d_name'],
            'am_level'=>$value['d_level'],
            'am_parent_id'=>$value['d_parent_id'],
            //'am_root_id'=>$value['d_root_id'],
            'am_root_id'=>0,
            'am_order'=>$value['d_order'],
            'create_dateline'=>$value['create_time'],
            'modify_dateline'=>$value['modify_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function dictionary_school(){
    global $_G;
    //@ini_set("display_errors","On");
    //@error_reporting(E_ALL);
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='dictionary';
    $new_table='school_main';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE d_type=1 AND isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE d_type=1 AND isdelete = 0";
    $result_count = DB::result_first($sql);
    $sql = "SELECT * FROM ".DB::table('school')." WHERE isdelete=0";
    $school = DB::fetch_all($sql);
    $school_array=array();
    foreach($school AS $key => $value){
        $school_array[$value['d_id']]=$value;
    }
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $detail = $school_array[$value['d_id']];
        $data = array(
            'sm_id'=>$value['d_id'],
            'sm_name'=>$value['d_name'],
            'sm_intro'=>$detail['school_intro'],
            'sm_contact'=>$detail['school_contact'],
            'sm_grade'=>$detail['school_grade'],
            'sm_img'=>_SITE_URL_.$detail['school_img'],
            'sm_status'=>$detail['school_status'],
            'sm_type'=>$detail['school_type']-1,
            'am_id_1'=>$detail['area_id1'],
            'am_id_2'=>$detail['area_id2'],
            'am_id_3'=>$detail['area_id3'],
            'create_dateline'=>$value['create_time'],
            'modify_dateline'=>$value['modify_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function dictionary_school_online(){
    global $_G;
    //@ini_set("display_errors","On");
    //@error_reporting(E_ALL);
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='dictionary';
    $new_table='school_online';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE d_type=2 AND isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE d_type=2 AND isdelete = 0";
    $result_count = DB::result_first($sql);
    $sql = "SELECT * FROM ".DB::table('school')." WHERE isdelete=0";
    $school = DB::fetch_all($sql);
    $school_array=array();
    foreach($school AS $key => $value){
        $school_array[$value['d_id']]=$value;
    }
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $detail = $school_array[$value['d_id']];
        $data = array(
            'so_id'=>$value['d_id'],
            'so_name'=>$value['d_name'],
            'so_intro'=>$detail['school_intro'],
            'so_contact'=>$detail['school_contact'],
            'so_grade'=>$detail['school_grade'],
            'so_img'=>_SITE_URL_.$detail['school_img'],
            'so_status'=>$detail['school_status'],
            'so_type'=>$detail['school_type']-1,
            'so_domain'=>$detail['school_domain'],
            'am_id_1'=>$detail['area_id1'],
            'am_id_2'=>$detail['area_id2'],
            'am_id_3'=>$detail['area_id3'],
            'create_dateline'=>$value['create_time'],
            'modify_dateline'=>$value['modify_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function content(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='content';
    $new_table='content_info';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data = $value;
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function announcement(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='announcement';
    $new_table='content_info';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE status=2";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE status=2";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data=array(
            'info_id'=>$value['id']+100,
            'info_cateid'=>1006,
            'title'=>$value['title'],
            'content'=>htmlspecialchars_decode($value['content']),
            'create_dateline'=>$value['dateline'],
            'modify_dateline'=>$value['publish_time'],
            'so_id'=>$value['org_id'],
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束   
}

function account_shipping_address(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='account_shipping_address';
    $new_table='user_shipping_address';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete=0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete=0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data=array(
            'usa_id'=>$value['msa_id'],
            'usa_user_realname'=>$value['msa_user_realname'],
            'user_id'=>$value['user_id'],
            'usa_user_phone'=>$value['msa_user_phone'],
            'usa_address'=>$value['msa_address'],
            'usa_lat'=>$value['ms_lat'],
            'usa_lng'=>$value['ms_lng'],
            'usa_isdefault'=>'1',
            'create_dateline'=>$value['create_dateline'],
            'modify_dateline'=>$value['modify_dateline'],
            'am_id_1'=>'0',
            'am_id_2'=>'0',
            'am_id_3'=>'0'
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function files(){
    global $_G;    
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='files';
    $new_table='file_source';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete=0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete=0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data=array(
            'fs_id'=>$value['file_id'],
            'fs_name'=>$value['file_name'],
            'fs_extension'=>$value['file_extension'],
            'fs_description'=>$value['file_description'],
            'fs_store_path'=>$value['file_store_path'],
            'fs_real_name'=>$value['file_real_name'],
            'fs_size'=>$value['file_size'],
            'fs_views'=>$value['file_views'],
            'fs_downs'=>$value['file_downs'],
            'fs_restype'=>$value['restype'],
            'fs_is_checked_userid'=>$value['is_checked_userid'],
            'fs_is_checked_time'=>$value['is_checked_time'],
            'fs_no_checked_reason'=>$value['no_checked_reason'],
            'fs_is_public'=>$value['is_public'],
            'fs_file_star'=>$value['file_star'],
            'fs_grade_id'=>$value['grade_id'],
            'fs_subject_id'=>$value['subject_id'],
            'am_id_1'=>$value['file_name'],
            'am_id_2'=>$value['file_name'],
            'am_id_3'=>$value['file_name'],
            'user_id'=>$value['uid'],
            'create_dateline'=>$value['file_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function course_cate(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='dictionary';
    $new_table='course_cate';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE d_root_id=200 AND d_id<>200 AND isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE d_root_id=200 AND d_id<>200 AND isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        if($value['d_parent_id']==200){
            $value['d_parent_id']=0;
        }
        $data = array(
            'cc_id'=>$value['d_id'],
            'cc_type'=>'1',
            'cc_name'=>$value['d_name'],
            'cc_level'=>$value['d_level'],
            'cc_parent_id'=>$value['d_parent_id'],
            'cc_root_id'=>0,
            'cc_order'=>$value['d_order'],
            'create_dateline'=>$value['create_time'],
            'modify_dateline'=>$value['modify_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function famous_teachers(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='famous_teachers';
    $new_table='famous_teachers';
    $sql = "SELECT * FROM ".DB::table($old_table)."";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)."";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $value['create_dateline']=$value['dateline'];
        $value['so_id']=$value['dept_id'];
        $value['brife']=htmlspecialchars_decode($value['profile']);
        $value['profile']=htmlspecialchars_decode($value['profile']);
        $value['brife']=strip_tags($value['profile']);
        $value['avatar']=_SITE_URL_.strip_tags($value['avatar']);
        $value['profile']=strip_tags($value['profile']);
        $value['information']=htmlspecialchars_decode($value['information']);
        unset($value['dateline']);
        unset($value['dept_id']);
        $data = $value;
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function course_cate_custom(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='course_cate_custom';
    $new_table='course_cate';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data = array(
            'cc_id'=>$value['ccc_id'],
            'cc_type'=>'3',
            'cc_name'=>$value['ccc_name'],
            'cc_level'=>1,
            'cc_parent_id'=>0,
            'cc_root_id'=>0,
            'cc_order'=>0,
            'create_dateline'=>$value['create_time'],
            'modify_dateline'=>$value['modify_time']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function course(){
	global $_G;
	@set_time_limit(0);
	@set_magic_quotes_runtime(0);
	@ini_set('memory_limit','2048M');
	//DEBUG 导入 开始
	//连接老数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
	//DEBUG 获取用户表数据
	$old_table='course';
	$new_table='course_main';
	$sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids=0";
	$result = DB::fetch_all($sql);
	$sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids=0";
	$result_count = DB::result_first($sql);
	foreach($result AS $key => $value){
		//DEBUG 课程所属网校字段 dept_id 转换为 so_id
		$sql="SELECT d_id FROM ".DB::table('dictionary')." WHERE d_type=2 AND d_id ='".$value['dept_id']."' LIMIT 1";
		$so_id = DB::result_first($sql);
		if($so_id){
			$result[$key]['dept_id']=$so_id;
		}else{
			$result[$key]['dept_id']=0;
		}
	}
	//连接新数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
	$import_count = 0;
	foreach($result AS $key => $value){
		$data = array(
				'cm_id'=>$value['id'],
				'so_id'=>$value['dept_id'],
				'user_id'=>$value['create_id'],
				'cm_name'=>$value['name'],
				'cm_img'=>_SITE_URL_.$value['title_page'],
				'cm_price'=>$value['price'],
				'cm_deadlinenotify'=>$value['deadlinenotify'],
				'cm_introduction'=>$value['introduction'],
				'cm_brief'=>$value['brief'],
				'cm_start_time'=>$value['start_time'],
				'cm_end_time'=>$value['end_time'],
				'cm_buy_starttime'=>'',
				'cm_buy_endtime'=>'',
				'cm_flag'=>$value['flag'],
				'cm_showstudentnumtype'=>0,
				'cm_recommend'=>$value['recommend'],
				'cm_related_id'=>$value['related_id'],
				'cm_frontpage_sort'=>$value['frontpage_sort'],
				'cm_cate_id'=>1,
				'cm_hour_num'=>$value['course_hour_num'],
				'cm_grade_id'=>$value['grade_id'],
				'cm_subject_id'=>$value['subject_id'],
				'cm_cc_id_1_1'=>$value['course_cateid1'],
				'cm_cc_id_1_2'=>$value['course_cateid2'],
				'cm_cc_id_1_3'=>$value['course_cateid3'],
				'cm_cc_id_2_1'=>'',
				'cm_cc_id_2_2'=>'',
				'cm_cc_id_2_3'=>'',
				'cm_cc_id_3_1'=>$value['course_cate_custom'],
				'cm_cc_id_3_2'=>'',
				'cm_cc_id_3_3'=>'',
				'create_dateline'=>$value['dateline'],
				'modify_dateline'=>'',
				'isdelete'=>0,
				'good_review'=>0,
				'cm_count'=>0,
				'cm_term_id'=>$value['term'],
				'cm_difficult'=>$value['term'],
				'cm_term_cate'=>$value['course_cateid'],
				'cm_issue'=>$value['issue'],
				'is_group'=>''
		);
		$insert_id = DB::insert($new_table, $data, true, true);
		if($insert_id){
			$import_count++;
		}
	}
	$log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
	$log .= 'import total count = '.$import_count.PHP_EOL;
	$import_status='failed';
	if($import_count==$result_count){
		$import_status='success';
	}
	return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
	//DEBUG 导入 结束
}

function course_group(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='course';
    $new_table='course_main';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids > 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids > 0";
    $result_count = DB::result_first($sql);
    foreach($result AS $key => $value){
        //DEBUG 课程所属网校字段 dept_id 转换为 so_id
        $sql="SELECT d_id FROM ".DB::table('dictionary')." WHERE d_type=2 AND d_id ='".$value['dept_id']."' LIMIT 1";
        $so_id = DB::result_first($sql);
        if($so_id){
           $result[$key]['dept_id']=$so_id;
        }else{
           $result[$key]['dept_id']=0; 
        }
    }
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data = array(
            'cm_id'=>$value['id'],
            'so_id'=>$value['dept_id'],
            'user_id'=>$value['create_id'],
            'cm_name'=>$value['name'],
            'cm_img'=>_SITE_URL_.$value['title_page'],
            'cm_price'=>$value['price'],
            'cm_deadlinenotify'=>$value['deadlinenotify'],
            'cm_introduction'=>$value['introduction'],
            'cm_brief'=>$value['brief'],
            'cm_start_time'=>$value['start_time'],
            'cm_end_time'=>$value['end_time'],
            'cm_buy_starttime'=>'',
            'cm_buy_endtime'=>'',
            'cm_flag'=>$value['flag'],
            'cm_showstudentnumtype'=>0,
            'cm_recommend'=>$value['recommend'],
            'cm_related_id'=>$value['related_id'],
            'cm_frontpage_sort'=>$value['frontpage_sort'],
            'cm_cate_id'=>1,
            'cm_hour_num'=>$value['course_hour_num'],
            'cm_grade_id'=>$value['grade_id'],
            'cm_subject_id'=>$value['subject_id'],
            'cm_cc_id_1_1'=>$value['course_cateid1'],
            'cm_cc_id_1_2'=>$value['course_cateid2'],
            'cm_cc_id_1_3'=>$value['course_cateid3'],
            'cm_cc_id_2_1'=>'',
            'cm_cc_id_2_2'=>'',
            'cm_cc_id_2_3'=>'',
            'cm_cc_id_3_1'=>$value['course_cate_custom'],
            'cm_cc_id_3_2'=>'',
            'cm_cc_id_3_3'=>'',
            'create_dateline'=>$value['dateline'],
            'modify_dateline'=>'',
            'isdelete'=>0,
            'good_review'=>0,
            'cm_count'=>0,
            'cm_term_id'=>$value['term'],
            'cm_difficult'=>0,
            'cm_term_cate'=>$value['course_cateid'],
            'cm_issue'=>$value['issue'],
            'is_group'=>1
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
        //DEBUG 插入组合课程关系
        if($value['group_course_ids']){
            $group_course_ids_array = explode(',',$value['group_course_ids']);
            foreach($group_course_ids_array AS $kk=>$vv){
                $course_group_lesson_data = array('cg_id'=>$vv,'cm_id'=>$value['id']);
                @DB::insert('course_group_lesson',$course_group_lesson_data, true, true);
            }
        }
    }
    $log .= 'get total '.$old_table.'groups = '.$result_count.PHP_EOL;
    $log .= 'import total groups count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function course_hour(){
    global $_G;
	@set_time_limit(0);
	@set_magic_quotes_runtime(0);
	@ini_set('memory_limit','2048M');
	//DEBUG 导入 开始
	//连接老数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
	//DEBUG 获取用户表数据
	$old_table='course_hour';
	$new_table='course_hour';
	$sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
	$result = DB::fetch_all($sql);
	$sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
	$result_count = DB::result_first($sql);
	//连接新数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
	$import_count = 0;
	foreach($result AS $key => $value){
		//DEBUG 判断是否存在课程章节 没有就插入一个默认章节
		$sql="SELECT cc_id FROM ".DB::table('course_chapter')." WHERE cm_id='".$value['course_id']."' LIMIT 1";
		$cc_id = DB::result_first($sql);
		if(empty($cc_id)){
			//DEBUG 插入默认章节
			$data=array(
					'cm_id'=>$value['course_id'],
					'cc_name'=>'默认章节',
					'cc_sort'=>1,
					'cc_level'=>1,
					'cc_pid'=>0,
					'create_dateline'=>TIMESTAMP
			);
			$cc_id = DB::insert('course_chapter', $data, true, true);
		}
		$data = array(
				'ch_id'=>$value['id'],
				'ch_name'=>$value['name'],
				'ch_duration'=>$value['duration'],
				'ch_img'=>_SITE_URL_.$value['title_page'],
				'ch_files'=>$value['files'],
				'ch_student_files'=>$value['student_files'],
				'ch_teacher_id'=>$value['teacher_id'],
				'cm_id'=>$value['course_id'],
				'cc_id'=>$cc_id,//所属章节编号
				'ch_sort'=>0,//课时排序
				'ch_introduction'=>$value['introduction'],
				'ch_start_time'=>$value['start_time'],
				'ch_end_time'=>$value['end_time'],
				'user_id'=>$value['create_id'],
				'ch_vod_url'=>$value['vod_url'],
				'ch_grade_id'=>$value['ch_grade_id'],
				'ch_subject_id'=>$value['subject_id'],
				'ch_type'=>1,
				'create_dateline'=>$value['create_dateline'],
				'isdelete'=>''
		);
		$insert_id = DB::insert($new_table, $data, true, true);
		if($insert_id){
			//DEBUG 判断是否存在课程章节 没有就插入一个默认章节
			$sql="SELECT chc_id FROM ".DB::table('course_hour_chapter')." WHERE ch_id='".$value['id']."' AND cc_id_1='".$cc_id."' LIMIT 1";
			$chc_id = DB::result_first($sql);
			if(empty($chc_id)){
				//DEBUG 插入课时章节关系表
				$data = array(
						'ch_id'=>$value['id'],
						'cc_id_1'=>$cc_id,
						'cc_id_2'=>0,
						'chc_sort'=>0,
						'create_dateline'=>TIMESTAMP
				);
				DB::insert('course_hour_chapter', $data, true, true);
			}
			$import_count++;
		}
	}
	$log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
	$log .= 'import total count = '.$import_count.PHP_EOL;
	$import_status='failed';
	if($import_count==$result_count){
		$import_status='success';
	}
	return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
	//DEBUG 导入 结束
}
function course_vod(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='vod';
    $new_table='course_main';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids=0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids=0";
    $result_count = DB::result_first($sql);
    foreach($result AS $key => $value){
        //DEBUG 课程所属网校字段 dept_id 转换为 so_id
        $sql="SELECT d_id FROM ".DB::table('dictionary')." WHERE d_type=2 AND d_id ='".$value['dept_id']."' LIMIT 1";
        $so_id = DB::result_first($sql);
        if($so_id){
            $result[$key]['dept_id']=$so_id;
        }else{
           $result[$key]['dept_id']=0; 
        }
    }
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data = array(
            'cm_id'=>$value['id']+1000,
            'so_id'=>$value['dept_id'],
            'user_id'=>$value['create_id'],
            'cm_name'=>$value['name'],
            'cm_img'=>_SITE_URL_.$value['title_page'],
            'cm_price'=>$value['price'],
            'cm_deadlinenotify'=>$value['deadlinenotify'],
            'cm_introduction'=>$value['introduction'],
            'cm_brief'=>$value['brief'],
            'cm_start_time'=>$value['start_time'],
            'cm_end_time'=>$value['end_time'],
            'cm_buy_starttime'=>'',
            'cm_buy_endtime'=>'',
            'cm_flag'=>$value['flag'],
            'cm_showstudentnumtype'=>0,
            'cm_recommend'=>$value['recommend'],
            'cm_related_id'=>$value['related_id'],
            'cm_frontpage_sort'=>$value['frontpage_sort'],
            'cm_cate_id'=>1,
            'cm_hour_num'=>$value['course_hour_num'],
            'cm_grade_id'=>$value['grade_id'],
            'cm_subject_id'=>$value['subject_id'],
            'cm_cc_id_1_1'=>$value['course_cateid1'],
            'cm_cc_id_1_2'=>$value['course_cateid2'],
            'cm_cc_id_1_3'=>$value['course_cateid3'],
            'cm_cc_id_2_1'=>'',
            'cm_cc_id_2_2'=>'',
            'cm_cc_id_2_3'=>'',
            'cm_cc_id_3_1'=>$value['course_cate_custom'],
            'cm_cc_id_3_2'=>'',
            'cm_cc_id_3_3'=>'',
            'create_dateline'=>$value['dateline'],
            'modify_dateline'=>'',
            'isdelete'=>0,
            'good_review'=>0,
            'cm_count'=>0,
            'cm_term_id'=>$value['term'],
            'cm_difficult'=>0,
            'cm_term_cate'=>$value['course_cateid']
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total vod '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total vod count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function course_vod_group(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='vod';
    $new_table='course_main';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids > 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0 AND group_course_ids > 0";
    $result_count = DB::result_first($sql);
    foreach($result AS $key => $value){
        //DEBUG 课程所属网校字段 dept_id 转换为 so_id
        $sql="SELECT d_id FROM ".DB::table('dictionary')." WHERE d_type=2 AND d_id ='".$value['dept_id']."' LIMIT 1";
        $so_id = DB::result_first($sql);
        if($so_id){
           $result[$key]['dept_id']=$so_id;
        }else{
           $result[$key]['dept_id']=0; 
        }
    }
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        $data = array(
            'cm_id'=>$value['id']+1000,
            'so_id'=>$value['dept_id'],
            'user_id'=>$value['create_id'],
            'cm_name'=>$value['name'],
            'cm_img'=>_SITE_URL_.$value['title_page'],
            'cm_price'=>$value['price'],
            'cm_deadlinenotify'=>$value['deadlinenotify'],
            'cm_introduction'=>$value['introduction'],
            'cm_brief'=>$value['brief'],
            'cm_start_time'=>$value['start_time'],
            'cm_end_time'=>$value['end_time'],
            'cm_buy_starttime'=>'',
            'cm_buy_endtime'=>'',
            'cm_flag'=>$value['flag'],
            'cm_showstudentnumtype'=>0,
            'cm_recommend'=>$value['recommend'],
            'cm_related_id'=>$value['related_id'],
            'cm_frontpage_sort'=>$value['frontpage_sort'],
            'cm_cate_id'=>1,
            'cm_hour_num'=>$value['course_hour_num'],
            'cm_grade_id'=>$value['grade_id'],
            'cm_subject_id'=>$value['subject_id'],
            'cm_cc_id_1_1'=>$value['course_cateid1'],
            'cm_cc_id_1_2'=>$value['course_cateid2'],
            'cm_cc_id_1_3'=>$value['course_cateid3'],
            'cm_cc_id_2_1'=>'',
            'cm_cc_id_2_2'=>'',
            'cm_cc_id_2_3'=>'',
            'cm_cc_id_3_1'=>$value['course_cate_custom'],
            'cm_cc_id_3_2'=>'',
            'cm_cc_id_3_3'=>'',
            'create_dateline'=>$value['dateline'],
            'modify_dateline'=>'',
            'isdelete'=>0,
            'good_review'=>0,
            'cm_count'=>0,
            'cm_term_id'=>$value['term'],
            'cm_difficult'=>0,
            'cm_term_cate'=>$value['course_cateid'],
            'is_group'=>1
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
        //DEBUG 插入组合课程关系
        if($value['group_course_ids']){
            $group_course_ids_array = explode(',',$value['group_course_ids']);
            foreach($group_course_ids_array AS $kk=>$vv){
                $course_group_lesson_data = array('cg_id'=>$vv+1000,'cm_id'=>$value['id']+1000);
                @DB::insert('course_group_lesson',$course_group_lesson_data, true, true);
            }
        }
    }
    $log .= 'get total vod group '.$old_table.'groups = '.$result_count.PHP_EOL;
    $log .= 'import total vod group count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function course_vod_hour(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='vod_hour';
    $new_table='course_hour';
    $sql = "SELECT * FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)." WHERE isdelete = 0";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        //DEBUG 判断是否存在课程章节 没有就插入一个默认章节
        $sql="SELECT cc_id FROM ".DB::table('course_chapter')." WHERE cm_id='".($value['v_id']+1000)."' LIMIT 1";
        $cc_id = DB::result_first($sql);
        if(empty($cc_id)){
           //DEBUG 插入默认章节
           $data=array(
                'cm_id'=>($value['v_id']+1000),
                'cc_name'=>'默认章节',
                'cc_sort'=>1,
                'cc_level'=>1,
                'cc_pid'=>0,
                'create_dateline'=>TIMESTAMP
           );
           $cc_id = DB::insert('course_chapter', $data, true, true);
        }
        $data = array(
            'ch_id'=>($value['id']+2000),
            'ch_name'=>$value['name'],
            'ch_duration'=>$value['duration'],
            'ch_img'=>$value['title_page'],
            'ch_files'=>$value['files_path'],
            'ch_student_files'=>$value['files_path'],
            'ch_teacher_id'=>$value['teacher_id'],
            'cm_id'=>($value['v_id']+1000),
            'cc_id'=>$cc_id,//所属章节编号
            'ch_sort'=>0,//课时排序
            'ch_introduction'=>$value['introduction'],
            'ch_start_time'=>$value['start_time'],
            'ch_end_time'=>$value['end_time'],
            'user_id'=>$value['create_id'],
            'ch_vod_url'=>$value['vod_url'],
            'ch_grade_id'=>0,
            'ch_subject_id'=>0,
            'ch_type'=>2,
            'create_dateline'=>$value['create_dateline'],
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            //DEBUG 判断是否存在课程章节 没有就插入一个默认章节
            $sql="SELECT chc_id FROM ".DB::table('course_hour_chapter')." WHERE ch_id='".($value['id']+2000)."' AND cc_id_1='".$cc_id."' LIMIT 1";
            $chc_id = DB::result_first($sql);
            if(empty($chc_id)){
                //DEBUG 插入课时章节关系表
                $data = array(
                    'ch_id'=>($value['id']+2000),
                    'cc_id_1'=>$cc_id,
                    'cc_id_2'=>0,
                    'chc_sort'=>0,
                    'create_dateline'=>TIMESTAMP
                );
                DB::insert('course_hour_chapter', $data, true, true);
            }
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

//订单主表旧数据修复 2017-2-22 13:45:35
function trade(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit', '2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';
    DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table = 'trade';
    $new_table = 'trade_main';

    // MARK: 以订单主表作为扫描对象
    $start = 0;
    // 定义步长
    $stepSize = 5000;
    // 获取 旧表 数据总数
    $sql_cnt_old = "select count(*) as cnt from ".DB::table($old_table)." where isdelete = 0 ";
    $result_count = $count_old = DB::result_first($sql_cnt_old);//旧数据条数
    $import_count = 0;//成功导入的条数
    //订单状态转换关系
    $tm_all_status = [
        'REFUND_CLOSED'     =>  '7',
        'REFUND_SUCCESS'    =>  '7',
        'TRADE_CLOSED'      =>  '7',
        'TRADE_FINISHED'    =>  '2',
        'TRADE_FREE'        =>  '2',
        'TRADE_PART_SUCCESS'=>  '2',
        'TRADE_SUCCESS'     =>  '2',
        'WAIT_BUYER_PAY'    =>  '1'
    ];

    while($count_old > 0){//如果还有尚未处理的数据
        DB::init($driver, $_G['config']['old_db']);//切换数据库连接句柄到旧库
        // 按照步长吞入内存数据
        $resArr = [];//定义接收结果池

        $sql_res = "select T.*, U.dept_id "
                . " from ".DB::table($old_table)." as T "
                . " left join ".DB::table('users')." as U on U.user_id = T.user_id "
                . " where T.isdelete = 0 "
                . " limit ".$start.", ".$stepSize;
        $resArr = DB::fetch_all($sql_res);

        //连接新数据库
        DB::init($driver, $_G['config']['db']);
        if(!empty($resArr) && is_array($resArr)){
            foreach ($resArr as $item) {//遍历目标结果集重新组装数据
                $data = [
                    'tm_id'                 =>  $item['trade_id'],
                    'user_id'               =>  $item['user_id'],
                    'so_id'                 =>  $item['dept_id'],
                    'tm_out_trade_no'       =>  $item['out_trade_no'],
                    'tm_youhui'             =>  0,
                    'tm_total_price'        =>  $item['total_fee'],
                    'tm_total_fee'          =>  $item['total_fee'],
                    'tm_trade_no'           =>  $item['trade_no'],
                    'tm_status'             =>  $item['trade_status'],
                    'tm_way'                =>  $item['trade_way'],
                    'tm_bank_code'          =>  $item['trade_bank_code'],
                    'usa_id'                =>  0,
                    'tm_express_number'     =>  0,
                    'tm_express_status'     =>  0,
                    'tm_evaluate'           =>  2,
                    'create_dateline'       =>  $item['create_dateline'],
                    'modify_dateline'       =>  $item['modify_dateline'],
                    'is_free'               =>  0,
                    'tm_express_address'    =>  0,
                    'tm_all_status'         =>  $tm_all_status[$item['trade_status']],
                    'tm_paytime'            =>  $item['first_trade_success_time']
                ];

                $insert_id = DB::insert($new_table, $data, true, true);
                if($insert_id){
                    $import_count += 1;
                }
            }
        }

        $start += $stepSize;//数据查询偏移量为步长
        $count_old -= $stepSize;//待处理数据量减少为步长

        sleep(1);//程序休息1秒
    }

    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    return $log;    
    //DEBUG 导入 结束
}
//订单汇款单旧数据修复 2017-2-22 14:44:14
function trade_bill(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';
    DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table = 'trade_bill';
    $new_table = 'trade_bill';

    // MARK: 以订单主表作为扫描对象
    $start = 0;
    // 定义步长
    $stepSize = 5000;
    // 获取 旧表 数据总数
    $sql_cnt_old = "select count(*) as cnt from ".DB::table($old_table);
    $result_count = $count_old = DB::result_first($sql_cnt_old);//旧数据条数
    $import_count = 0;//成功导入的条数

    while($count_old > 0){//如果还有尚未处理的数据
        DB::init($driver, $_G['config']['old_db']);//切换数据库连接句柄到旧库
        // 按照步长吞入内存数据
        $resArr = [];//定义接收结果池

        $sql_res = "select * "
                . " from ".DB::table($old_table)." limit ".$start.", ".$stepSize;
        $resArr = DB::fetch_all($sql_res);

        //连接新数据库
        DB::init($driver, $_G['config']['db']);
        if(!empty($resArr) && is_array($resArr)){
            foreach ($resArr as $item) {//遍历目标结果集重新组装数据
                $data = [
                    'tb_id'                 =>  $item['tb_id'],
                    'tb_user_id'            =>  $item['tb_user_id'],
                    'tb_img'                =>  $item['tb_img'],
                    'tb_user_account'       =>  $item['tb_user_account'],
                    'tb_user_name'          =>  $item['tb_user_name'],
                    'tb_money'              =>  $item['tb_money'],
                    'tb_time'               =>  $item['tb_time'],
                    'tb_mobile'             =>  $item['tb_mobile'],
                    'tb_other'              =>  $item['tb_other'],
                    'tb_trade_id'           =>  $item['tb_trade_id'],
                    'modify_dateline'       =>  $item['create_time'],
                    'create_dateline'       =>  $item['create_time']
                ];

                $insert_id = DB::insert($new_table, $data, true, true);
                if($insert_id){
                    $import_count += 1;
                }
            }
        }

        $start += $stepSize;//数据查询偏移量为步长
        $count_old -= $stepSize;//待处理数据量减少为步长

        sleep(1);//程序休息1秒
    }

    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
    return $log;
}
//订单详情旧数据修复 2017-2-22 14:55:50
function trade_course(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';
    DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table = 'student_course';
    $new_table = 'trade_course';

    // MARK: 以订单主表作为扫描对象
    $start = 0;
    // 定义步长
    $stepSize = 5000;
    // 获取 旧表 数据总数
    $sql_cnt_old = "select count(*) as cnt from ".DB::table($old_table)." where course_flag in (1,2)";
    $result_count = $count_old = DB::result_first($sql_cnt_old);//旧数据条数
    $import_count = 0;//成功导入的条数

    while($count_old > 0){//如果还有尚未处理的数据
        DB::init($driver, $_G['config']['old_db']);//切换数据库连接句柄到旧库
        // 按照步长吞入内存数据
        $resArr = [];//定义接收结果池

        $sql_res = "select * "
                . " from ".DB::table($old_table)
                . " where course_flag in (1,2) "
                . " limit ".$start.", ".$stepSize;
        $resArr = DB::fetch_all($sql_res);

        //连接新数据库
        DB::init($driver, $_G['config']['db']);
        if(!empty($resArr) && is_array($resArr)){
            foreach ($resArr as $item) {//遍历目标结果集重新组装数据
                if($item['course_cate'] == 2){//点播
                    $cm_id = $item['course_id'] + 1000;
                }
                else{
                    $cm_id = $item['course_id'];
                }
                
                $data = [
                    'tc_id'                 =>  $item['id'],
                    'user_id'               =>  $item['uid'],
                    'cm_id'                 =>  $cm_id,
                    'group_cm_id'           =>  0,
                    'cm_price'              =>  $item['course_price'],
                    'tc_flag'               =>  $item['course_flag'] + 1,
                    'tc_type'               =>  $item['course_type'],
                    'tm_id'                 =>  $item['trade_id'],
                    'tc_applicant'          =>  $item['applicant'],
                    'tc_take_handout'       =>  1,
                    'modify_dateline'       =>  $item['dateline'],
                    'create_dateline'       =>  $item['dateline']
                ];

                $insert_id = DB::insert($new_table, $data, true, true);
                if($insert_id){
                    $import_count += 1;
                }
            }
        }

        $start += $stepSize;//数据查询偏移量为步长
        $count_old -= $stepSize;//待处理数据量减少为步长

        sleep(1);//程序休息1秒
    }

    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
    return $log;
}
//免费订单数据修复 2017-6-9 13:08:45
function trade_course_free(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接新数据库
    $driver = 'db_driver_mysql';
    DB::init($driver, $_G['config']['db']);
    //DEBUG 获取表数据
    $courseTable = 'trade_course';
    //获取所有状态为 免费订单 的数据
    $sql_res = "select * "
                . " from ".DB::table($courseTable)
                . " where tc_flag = 3 ";
    $resArr = DB::fetch_all($sql_res);
    
    if(!empty($resArr) && is_array($resArr)){
        foreach ($resArr as $item) {//遍历目标结果集重新组装数据
            //1.找到对应的tm_id将对应的 trade_main 的 is_free 字段更新为 1
            $mdata['is_free'] = 1;
            $mwhere = ['tm_id' => $item['tm_id']];
            @DB::update('trade_main', $mdata, $mwhere);
            //2.更新原本的记录 tc_flag 为 2
            $cdata['tc_flag'] = 2;
            $cwhere = ['tc_id' => $item['tc_id']];
            @DB::update('trade_course', $cdata, $cwhere);
        }
    }
    
    $log = '免费订单数据修复完成'.PHP_EOL;
    //DEBUG 导入 结束
    return $log;
}
//订单补差价支付记录表数据修复 2017-2-22 15:51:03
function trade_diff_info(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';
    DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table = 'trade_diff_info';
    $new_table = 'trade_diff_info';

    // MARK: 以订单主表作为扫描对象
    $start = 0;
    // 定义步长
    $stepSize = 5000;
    // 获取 旧表 数据总数
    $sql_cnt_old = "select count(*) as cnt from ".DB::table($old_table)." where isdelete = 0 ";
    $result_count = $count_old = DB::result_first($sql_cnt_old);//旧数据条数
    $import_count = 0;//成功导入的条数

    while($count_old > 0){//如果还有尚未处理的数据
        DB::init($driver, $_G['config']['old_db']);//切换数据库连接句柄到旧库
        // 按照步长吞入内存数据
        $resArr = [];//定义接收结果池

        $sql_res = "select * "
                . " from ".DB::table($old_table)
                . " where isdelete = 0 "
                . " limit ".$start.", ".$stepSize;
        $resArr = DB::fetch_all($sql_res);

        //连接新数据库
        DB::init($driver, $_G['config']['db']);
        if(!empty($resArr) && is_array($resArr)){
            foreach ($resArr as $item) {//遍历目标结果集重新组装数据
                $data = [
                    'tdi_id'             =>  $item['tdi_id'],
                    'user_id'            =>  $item['user_id'],
                    'tdi_price'          =>  $item['tdi_price'],
                    'tm_id'              =>  $item['trade_id'],
                    'tm_out_trade_no'    =>  $item['out_trade_no'],
                    'create_dateline'    =>  $item['create_dateline'],
                    'modify_dateline'    =>  $item['modify_dateline']
                ];

                $insert_id = DB::insert($new_table, $data, true, true);
                if($insert_id){
                    $import_count += 1;
                }
            }
        }

        $start += $stepSize;//数据查询偏移量为步长
        $count_old -= $stepSize;//待处理数据量减少为步长

        sleep(1);//程序休息1秒
    }

    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
    return $log;
}

function student_course_record(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='student_course_record';
    $new_table='course_hour_record';
    $sql = "SELECT * FROM ".DB::table($old_table)."";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)."";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        //DEBUG 获取用户的年级与班级
        $user_detail = $class_main = array();
        if($value['student_id']){
            $sql = "SELECT class_id FROM ".DB::table('user_detail')." WHERE user_id='".$value['student_id']."' LIMIT 1";
            $user_detail = DB::fetch_first($sql);            
        }
        if($user_detail['class_id']){
            $sql = "SELECT grade_num,class_num FROM ".DB::table('class_main')." WHERE class_id='".$user_detail['class_id']."' LIMIT 1";
            $class_main = DB::fetch_first($sql);  
        }
        $data=array(
            'chr_id'=>$value['id'],
            'tcr_id'=>$value['tcr_id'],
            'cm_id'=>$value['course_id'],
            'ch_id'=>$value['course_lesson_id'],
            'cm_subject_id'=>'',
            'user_id'=>$value['student_id'],
            'ue_area_id1'=>'',
            'ue_area_id2'=>'',
            'ue_area_id3'=>'',
            'so_id'=>'',
            'sm_id'=>$value['school_id'],
            'grade_id'=>$class_main['grade_num'],
            'class_id'=>$class_main['class_num'],
            'start_time'=>$value['start_time'],
            'end_time'=>$value['end_time'],
            'order_num'=>$value['order_num'],
            'time_difference'=>$value['time_difference'],
            'dateline'=>$value['dateline'],
            'add_time'=>'',
            'look_time'=>'',
            'is_looked'=>0
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
}

function vod_playback(){
    global $_G;
    @set_time_limit(0);
    @set_magic_quotes_runtime(0);
    @ini_set('memory_limit','2048M');
    //DEBUG 导入 开始
    //连接老数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
    //DEBUG 获取用户表数据
    $old_table='vod_user_playback';
    $new_table='course_hour_record';
    $sql = "SELECT * FROM ".DB::table($old_table)."";
    $result = DB::fetch_all($sql);
    $sql = "SELECT count(*) FROM ".DB::table($old_table)."";
    $result_count = DB::result_first($sql);
    //连接新数据库
    $driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
    $import_count = 0;
    foreach($result AS $key => $value){
        //DEBUG 获取用户的年级与班级
        $user_detail = $class_main = array();
        if($value['user_id']){
            $sql = "SELECT class_id,sm_id FROM ".DB::table('user_detail')." WHERE user_id='".$value['user_id']."' LIMIT 1";
            $user_detail = DB::fetch_first($sql);            
        }
        if($user_detail['class_id']){
            $sql = "SELECT grade_num,class_num FROM ".DB::table('class_main')." WHERE class_id='".$user_detail['class_id']."' LIMIT 1";
            $class_main = DB::fetch_first($sql);  
        }
        //DEBUG 获取课时所属课程
        if($value['vod_id']){
            $sql = "SELECT cm_id FROM ".DB::table('course_hour')." WHERE ch_id='".($value['vod_id']+2000)."' LIMIT 1";
            $course_hour = DB::fetch_first($sql);  
        }
        $data=array(
            'chr_id'=>$value['id']+200000,
            'tcr_id'=>'',
            'cm_id'=>$course_hour['cm_id'],
            'ch_id'=>($value['vod_id']+2000),
            'cm_subject_id'=>'',
            'user_id'=>$value['user_id'],
            'ue_area_id1'=>'',
            'ue_area_id2'=>'',
            'ue_area_id3'=>'',
            'so_id'=>'',
            'sm_id'=>$user_detail['sm_id'],
            'grade_id'=>$class_main['grade_num'],
            'class_id'=>$class_main['class_num'],
            'start_time'=>$value['start_time'],
            'end_time'=>$value['end_time'],
            'order_num'=>0,
            'time_difference'=>($value['end_time']-$value['start_time']),
            'dateline'=>$value['dateline'],
            'add_time'=>'',
            'look_time'=>'',
            'is_looked'=>0
        );
        $insert_id = DB::insert($new_table, $data, true, true);
        if($insert_id){
            $import_count++;
        }
    }
    $log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
    $log .= 'import total count = '.$import_count.PHP_EOL;
    $import_status='failed';
    if($import_count==$result_count){
        $import_status='success';
    }
    return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
    //DEBUG 导入 结束
	
}
function question(){
	global $_G;
	@set_time_limit(0);
	@set_magic_quotes_runtime(0);
	@ini_set('memory_limit','2048M');
	//DEBUG 导入 开始
	//连接老数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
	//DEBUG 获取用户表数据
	$old_table='questions';
	$new_table='questions';
	$sql = "SELECT * FROM ".DB::table($old_table);
	$result = DB::fetch_all($sql);
	$sql = "SELECT count(*) FROM ".DB::table($old_table);
	$result_count = DB::result_first($sql);
	
	//连接新数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
	$import_count = 0;
	foreach($result AS $key => $value){
		$data = array(
				'q_id'=>$value['q_id'],
				'user_id'=>$value['user_id'],
				'title'=>$value['title'],
				'content'=>$value['content'],
				'cm_id'=>$value['course_id'],
				'ch_id'=>$value['course_hour_id'],
				'questions_type'=>$value['questions_type'],
				'end_reply_uid'=>$value['end_reply_uid'],
				'end_reply_time'=>$value['end_reply_time'],
				'answer_num'=>$value['answer_num'],
				'browse_num'=>$value['browse_num'],
				'top'=>$value['top'],
				'highlight'=>$value['highlight'],
				'is_flag'=>$value['is_flag'],				
				'dateline'=>$value['dateline'],					
		);
		$insert_id = DB::insert($new_table, $data, true, true);
		if($insert_id){
			$import_count++;
		}
	}
	$log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
	$log .= 'import total count = '.$import_count.PHP_EOL;
	$import_status='failed';
	if($import_count==$result_count){
		$import_status='success';
	}
	return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
	//DEBUG 导入 结束
}

function answers(){
	global $_G;
	@set_time_limit(0);
	@set_magic_quotes_runtime(0);
	@ini_set('memory_limit','2048M');
	//DEBUG 导入 开始
	//连接老数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['old_db']);
	//DEBUG 获取用户表数据
	$old_table='answers';
	$new_table='answers';
	$sql = "SELECT * FROM ".DB::table($old_table);
	$result = DB::fetch_all($sql);
	$sql = "SELECT count(*) FROM ".DB::table($old_table);
	$result_count = DB::result_first($sql);

	//连接新数据库
	$driver = 'db_driver_mysql';DB::init($driver, $_G['config']['db']);
	$import_count = 0;
	foreach($result AS $key => $value){
		$data = array(
				'a_id'=>$value['a_id'],
				'user_id'=>$value['user_id'],
				'q_id'=>$value['q_id'],
				'content'=>$value['content'],
				'adoption'=>$value['adoption'],				
				'is_flag'=>$value['is_flag'],
				'dateline'=>$value['dateline'],
		);
		$insert_id = DB::insert($new_table, $data, true, true);
		if($insert_id){
			$import_count++;
		}
	}
	$log .= 'get total '.$old_table.' = '.$result_count.PHP_EOL;
	$log .= 'import total count = '.$import_count.PHP_EOL;
	$import_status='failed';
	if($import_count==$result_count){
		$import_status='success';
	}
	return $log .= 'import '.$new_table.' table '.$import_status.PHP_EOL;
	//DEBUG 导入 结束
}